package com.smsc.headend.task.engine.registry;

import com.smsc.headend.module.protocol.ProtocolType;
import com.smsc.headend.task.engine.service.push.ProtocolDataProcess;
import com.smsc.headend.task.engine.service.push.impl.ProtocolDataProcessCosemImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liangli
 * @date 2020/10/21
 */
@Component
@Slf4j
public class DataNotificationRegistry {

    @Autowired
    AutowireCapableBeanFactory autowireCapableBeanFactory;

    Map<Long, ProtocolDataProcess> protocolDataProcessList = new HashMap<>();

    ProtocolDataProcessCosemImpl protocolDataProcessCosemImpl;

    @PostConstruct
    public void load() {
        protocolDataProcessCosemImpl = new ProtocolDataProcessCosemImpl();
        autowireCapableBeanFactory.autowireBean(protocolDataProcessCosemImpl);
        protocolDataProcessList.put(ProtocolType.DLMS_COSEM_46_Protocol.getProtocolId(), protocolDataProcessCosemImpl);
        protocolDataProcessList.put(ProtocolType.DLMS_COSEM_47_Protocol.getProtocolId(), protocolDataProcessCosemImpl);
        protocolDataProcessList.put(ProtocolType.DLMS_Gateway_Protocol.getProtocolId(), protocolDataProcessCosemImpl);
    }

    public ProtocolDataProcess getService(Long protocolId) {
        ProtocolDataProcess protocolDataProcess =  protocolDataProcessList.get(protocolId);
        if (protocolDataProcess == null) {
            log.error("{} have no Protocol data processor", protocolId);
        }
        return protocolDataProcess;
    }
}
